package com.example.demo;

import android.media.Image;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.demo.Bean.WeatherBean;
import com.google.gson.Gson;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CityWeatherBlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CityWeatherBlankFragment extends BaseFragment implements View.OnClickListener{
    TextView tempTv,cityTv,conditionTv,windTv,tempRangeTv,dateTv,clothIndexTv,carIndexTv,
            coldIndexTv,sportIndexTv,raysIndexTv;
    ImageView dayIv;
    LinearLayout futureLayout;
    String url1=" http://jisuqgtq.market.alicloudapi.com/weather/query?city=%E5%8C%97%E4%BA%AC";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private List<WeatherBean.ResultBean.IndexBean> indexList;

    public CityWeatherBlankFragment() {
        // Required empty public constructor

    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CityWeatherBlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CityWeatherBlankFragment newInstance(String param1, String param2) {
        CityWeatherBlankFragment fragment = new CityWeatherBlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_city_weather_blank, container, false);

        // Inflate the layout for this fragment
        Bundle bundle=getArguments();
        String city=bundle.getString("city");
        String url=url1;
        loadData(url);
        return view;

    }

    @Override
    public void onSuccess(String result) {
       //解析并展示数据
   parseShowData(result);

    }

    private void parseShowData(String result) {
        //使用gson解析数据
        WeatherBean weatherBean=new Gson().fromJson(result, WeatherBean.class);
        WeatherBean.ResultBean resultBean=weatherBean.getResult(result);
        indexList = resultBean.getIndex();
        //TextView
        dateTv.setText(resultBean.getDate());
       cityTv.setText(resultBean.getCity());
       resultBean.getWeather();
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
    }

    private void initeView(View view){
        //初始化控件
        tempTv=view.findViewById(R.id.frag_tv_currenttemp);
        tempTv = view.findViewById(R.id.frag_tv_currenttemp);
        cityTv = view.findViewById(R.id.frag_tv_city);
        conditionTv = view.findViewById(R.id.frag_tv_condition);
        windTv = view.findViewById(R.id.frag_tv_wind);
        tempRangeTv = view.findViewById(R.id.frag_tv_temprange);
        dateTv = view.findViewById(R.id.frag_tv_date);
        clothIndexTv = view.findViewById(R.id.frag_index_tv_dress);
        carIndexTv = view.findViewById(R.id.frag_index_tv_washcar);
        coldIndexTv = view.findViewById(R.id.frag_index_tv_cold);
        sportIndexTv = view.findViewById(R.id.frag_index_tv_sport);
        raysIndexTv = view.findViewById(R.id.frag_index_tv_rays);
//        airIndexTv = view.findViewById(R.id.frag_index_tv_air);
        dayIv = view.findViewById(R.id.frag_iv_today);
        futureLayout = view.findViewById(R.id.frag_center_layout);
//        outLayout = view.findViewById(R.id.out_layout);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.frag_index_tv_dress:
                break;
            case R.id.frag_index_tv_washcar:
                break;
            case R.id.frag_index_tv_cold:
                break;
            case R.id.frag_index_tv_sport:
                break;
            case R.id.frag_index_tv_rays:
                break;
        }
    }
}